<template>
  <!--试卷列表页-->
  <div>
    <a-card class="contentFullHeight" :bodyStyle="{padding : '10px 10px'}">
      <!--筛选、导入导出-->
      <a-card :bordered="true" class="searchBox" :bodyStyle="{padding : '10px 24px'}">
        <a-form layout="inline">
          <a-form-item label="试卷名">
            <a-input style="width: 150px" v-model="searchParams.keywords"></a-input>
          </a-form-item>
          <!--<a-form-item label="考试时间">-->
            <!--<a-range-picker-->
              <!--:defaultValue="[$util.moment(searchParams.bgTm),$util.moment(searchParams.endTm)]"-->
              <!--:format="$defaultSettings.dateFormat"-->
              <!--@change="onChange"/>-->
          <!--</a-form-item>-->
          <a-form-item>
            <a-button type="primary" icon="search" @click="handleSearch">查询</a-button>
          </a-form-item>
          <a-form-item>
            <!--新增-->
            <a-button type="primary" icon="plus" @click="handleCreate">新增</a-button>
          </a-form-item>
          <!--<a-form-item>-->
            <!--<a-button type="primary" icon="download" @click="handleDownload" :loading="downloadLoading">导出</a-button>-->
          <!--</a-form-item>-->
        </a-form>
      </a-card>

      <!--列表-->
      <a-table
        :columns="columns"
        :dataSource="data"
        bordered
        size="small"
        rowKey="id"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <!--平均分-->
        <!--<template slot="avgGrade" slot-scope="text,record">-->
          <!--{{ text|avgGrade }}-->
        <!--</template>-->

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleUpdate(record)" href="javascript:;">编辑</a>
            <a-divider type="vertical"/>
            <a href="javascript:;" @click="showConfirm(record)">删除</a>
          </template>
        </span>
      </a-table>
      <!--新增/编辑弹窗-->
      <PaperPopup  @getTableDataForExam="getTableData" :mdl="mdl"></PaperPopup>
    </a-card>
  </div>
</template>

<script>
/* eslint-disable */
  import {queryExamList,selectExamDetailList,insertExamData,examDelete} from '@/api/examManage/testPaper.js'
  import moment from 'moment';
  import PaperPopup from './form'
  export default {
    components: {
      PaperPopup
    },
    data() {
      return {

        /*   -------搜索框--------*/
        searchParams: {
          selected:'exam_name',
          keywords:'',
//          bgTm: moment(new Date()).subtract(6, 'days').format('YYYY-MM-DD'),
          bgTm: '',
//          endTm: this.$defaultSettings.endTm,
          endTm: '',
        },

        /*   -------分页--------*/
        pagination: this.$defaultSettings.pagination ,

        /*   -------弹窗--------*/
        mdl: {
          dialogStatus: '',
          dialogTitleMap: {
            update: '编辑',
            create: '添加'
          },
          visible: false,
          title: '',
          id:'',
          formData: {
            "exam": {
              "endTime": moment(new Date()).subtract(6, 'days').format('YYYY-MM-DD'),
              "examName": "",
              "examRemark": "",
              "questionSource": 0,
              "startTime": moment(new Date()).subtract(6, 'days').format('YYYY-MM-DD'),
              "examObjCompanyType":'',
              "needStudyTime":16,
              "examTime": 120,
            },
            "questionLibs": [],
            "questionNum0": 1,
            "questionNum1": 1,
            "questionNum2": 1,
            "questionNum3": 0
          },
          labelCol: {
            xs: {
              span: 24
            }
            ,
            sm: {
              span: 5
            }
          }
          ,
          wrapperCol: {
            xs: {
              span: 24
            }
            ,
            sm: {
              span: 16
            }
          },
          formItemLayoutWithOutLabel: {
            wrapperCol: {
              xs: { span: 24, offset: 0 },
              sm: { span: 20, offset: 4 },
            },
          },
        },


        /*   -------表格--------*/
        loading: false,
        columns:[
          {
            title: '试卷名',
            dataIndex: 'examName',
            scopedSlots: {customRender: 'examName'}
          },
          {
            title: '考试时长(分钟)',
            dataIndex: 'examTime',
            scopedSlots: {customRender: 'examTime'}
          },
          {
            title: '开始时间',
            dataIndex: 'startTime',
            scopedSlots: {customRender: 'startTime'}
          },
          {
            title: '结束时间',
            dataIndex: 'endTime',
            scopedSlots: {customRender: 'endTime'}
          },
          {
            title: '总分',
            dataIndex: 'grade'
          },
          {
            title: '考试人数',
            dataIndex: 'examStudentCount'
          },
          {
            title: '最高分',
            dataIndex: 'maxGrade'
          },
          {
            title: '最低分',
            dataIndex: 'minGrade'
          },
//          {
//            title: '平均分',
//            dataIndex: 'avgGrade',
//            scopedSlots: {customRender: 'avgGrade'}
//          },
           {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: {customRender: 'action'}
          }
        ],
        data:[],

        // 下载
        downloadLoading: false,
      }
    },
    inject: ['upShow'],
    created() {

    },
    mounted() {
      this.getTableData()
    },
    watch: {

    },
    computed: {

    },
    methods: {
      moment,
      onChange(dates, dateStrings) {
        // console.log('From: ', dates[0], ', to: ', dates[1]);
        this.searchParams.bgTm = dateStrings[0];
        this.searchParams.endTm = dateStrings[1];
      },
      handleTableChange(pagination) {
        this.pagination = pagination
        this.getTableData()
      },

      getTableData() {
        this.loading = true
        const parameter = {
          _search: true,//是否分页查询
          alarm: 0,
          avgGrade: 0,
          bgTm: this.searchParams.bgTm,
          data: {},
          endTime: "",
          endTm: this.searchParams.endTm,
          errorRate: 0,
          examId: 0,
          examName: "",
          examStudentCount: 0,
          examTime: "",
          grade: 0,
          keywords:  this.searchParams.keywords,
          maxGrade: 0,
          minGrade: 0,
          nd: "",
          orderBy: 0,
          pageSize: this.pagination.pageSize || 10,
          pageNumber: this.pagination.current || 1,
          selected: this.searchParams.selected,
          selectedType: "",
          startTime: "",
          status: 0,
          type: ""
        }
        queryExamList(parameter).then(res => {
          this.loading = false
          this.pagination.total = res.data.total
          this.data = res.data.records
        }).
        catch(err => {
          console.log(err)
        })
      },
      handleSearch() {
        this.pagination.current = 1
        this.getTableData()
      },

      setDefaults() {
        this.mdl.id = ''
        this.mdl.formData = {
          "exam": {
            "endTime": moment(new Date()).subtract(6, 'days').format('YYYY-MM-DD'),
            "examName": "",
            "examRemark": "",
            "questionSource": 0,
            "startTime": moment(new Date()).subtract(6, 'days').format('YYYY-MM-DD'),
            "examObjCompanyType":'',
            "needStudyTime":16,
            "examTime": 120,
          },
          "questionLibs": [],
          "questionNum0": 1,
          "questionNum1": 1,
          "questionNum2": 1,
          "questionNum3": 0,
        }
      },
      //添加行
      handleCreate() {
        this.setDefaults()
        this.mdl.dialogStatus = 'create'
        this.mdl.visible = true
      },
      /**
       *新增列表展示数据 insertExamData
       * */
      //编辑当前行
      handleUpdate(row) {
        const parameter ={
          examId:row.examId
        }
        selectExamDetailList(parameter).then(res => {
          this.loading = false
          var datas = res.data[0]
          this.mdl.id = row.examId
          if(datas.questionLibs.indexOf(',')!=-1) {
            var questionLibs = datas.questionLibs.split(',')
          }else {
            var questionLibs = []
            questionLibs.push(datas.questionLibs)
          }
          this.mdl.formData = {
            "exam": {
                id:row.examId,
              "endTime": row.endTime,
              "examName": datas.examName,
              "examRemark": datas.examRemark,
              "questionSource": 0,
              "startTime": row.startTime,
              "examObjCompanyType":datas.examObjCompanyType,
              "needStudyTime":datas.needStudyTime,
              "examTime": row.examTime,
            },
            "questionLibs": questionLibs,
            "questionNum0": datas.singleChoiceCount,
            "questionNum1": datas.multiChoiceCount,
            "questionNum2": datas.judgeCount,
            "questionNum3": datas.shortAnswerCount,
          }
          this.mdl.dialogStatus = 'update'
          this.mdl.visible = true
//          console.log(this.mdl.formData)
        })
      },

      handleDelete(row) {
        console.log(row)
          examDelete(row.examId).then(res=>{
          if (res.code === 200) {
            this.modalLoading = false
            this.upShow(1, '删除成功')
            this.handleSearch()
          } else {
            this.modalLoading = false
            this.upShow(2, res.msg || '删除失败')
          }
        }).catch(err => {
          this.modalLoading = false
          this.upShow(2, '删除失败')
        })
      },
      showConfirm(val) {
        var that =this
        this.$confirm({
          title: '删除试卷?',
          content: '确定删除试卷'+val.examName+'?',
          onOk() {
            // return ((resolve, reject) => {
              that.handleDelete(val)
            // }).catch(() => console.log('Oops errors!'));
          },
          onCancel() {},
        });
      },
      //下载
      handleDownload() {
        this.downloadLoading = true
        let param  ={
          keywords: this.searchParams.keywords,
          bgTm:this.searchParams.bgTm,
          endTm:this.searchParams.endTm,
        }
        /*refundRecorDdown(param).then(res=> {
          downloadFile(res,"退款记录","xlsx")
          this.downloadLoading = false
        })*/
      }
    }
  }
</script>

<style scoped>

</style>